// pages/user/promotion/promotion.js
const app = getApp();

let api = require('../../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    totalScore: 0,
    promotionData: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight - app.globalData.navHeight
        });
      }
    });
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var token = wx.getStorageSync('TOKEN');
    that.getScore(token);
    that.getScoreList(token);
  },
  /**
   * 获取积分总数
   */
  getScore: function (token) {
    var that = this;
    api._get('/exte/score', '', token).then(res => {
      if (res.code == 200) {
        var totalScore = 0

        if (res.result.totalScore) {
          totalScore = res.result.totalScore;
        }
        that.setData({
          totalScore: totalScore
        });

      } else if (res.code > 400) {
        wx.navigateTo({
          url: '../login/login',
        })
      } else {
        wx.showToast({
          title: '网络忙请重试',
          icon: 'none',
        })
      }

    });
  },
  /**
   * 获取推广列表
   */
  getScoreList: function (token) {

    var that = this;
    api._get('/exte/list', '', token).then(res => {
      if (res.code == 200) {

        var list = res.result;
        list.forEach(function (value, index) {

          var createTime = value.createTime * 1000;
          list[index]['showCreateTime'] = app.formatDate(createTime, 'YYYY-MM-DD');
        });
        that.setData({
          promotionData: list
        });

      } else if (res.code > 400) {
        wx.navigateTo({
          url: '../login/login',
        })
      } else {
        wx.showToast({
          title: '网络忙请重试',
          icon: 'none',
        })
      }

    });
  }
})